package com.example.wupic.dialog;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn1;//普通对话框
    Button btn2;//单选对话框
    Button btn3;//多选对话框
    Button btn4;//列表对话框
    Button btn5;//列表对话框
    Button btn6;//列表对话框
    Button btn7;//列表对话框
    Button btn8;//列表对话框
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1= findViewById(R.id.dialog1);
        btn2= findViewById(R.id.dialog2);
        btn3= findViewById(R.id.dialog3);
        btn4= findViewById(R.id.dialog4);
        btn5= findViewById(R.id.dialog5);
        btn6= findViewById(R.id.dialog6);
        btn7= findViewById(R.id.dialog7);
        btn8= findViewById(R.id.dialog8);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("普通对话框");// 设置标题
                // builder.setIcon(R.drawable.ic_launcher);//设置图标
                builder.setMessage("我是内容");// 为对话框设置内容
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"取消按钮",Toast.LENGTH_LONG).show();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"确定按钮",Toast.LENGTH_LONG).show();
                    }
                });
                builder.create().show();// 使用show()方法显示对话框
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("单选对话框");
                final String[] items = { "男", "女" };// 创建一个存放选项的数组
                final boolean[] checkedItems = { true, false };// 存放选中状态，true为选中
                // ，false为未选中，和setSingleChoiceItems中第二个参数对应
                // 为对话框添加单选列表项
                // 第一个参数存放选项的数组，第二个参数存放默认被选中的项，第三个参数点击事件
                builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        for (int i = 0; i < checkedItems.length; i++) {
                            checkedItems[i] = false;
                        }
                        checkedItems[arg1] = true;
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        arg0.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        String str = "";
                        for (int i = 0; i < checkedItems.length; i++) {
                            if (checkedItems[i]) {
                                str = items[i];
                            }
                        }
                        Toast.makeText(MainActivity.this,"确定按钮",Toast.LENGTH_LONG).show();
                    }
                });
                builder.create().show();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("多选对话框");
                final String[] items = { "数学", "语文", "英语", "政治" };// 存放选项的数组
                final boolean[] checkedItems = { false, true, false, false };

                // 第一个参数选项，第二个参数选项的状态，第三个点击事件
                builder.setMultiChoiceItems(items, checkedItems,
                        new DialogInterface.OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1,
                                                boolean arg2) {
                                // TODO Auto-generated method stub
                                if (arg2) {
                                    checkedItems[arg1] = true;
                                } else {
                                    checkedItems[arg1] = false;
                                    Toast.makeText(MainActivity.this,"点击了"+items[arg1],Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        arg0.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        String str = "";
                        for (int i = 0; i < checkedItems.length; i++) {
                            if (checkedItems[i]) {
                                str = str + items[i];
                            }
                        }
                        Toast.makeText(MainActivity.this,"选择了"+str,Toast.LENGTH_LONG).show();
                    }
                });
                builder.create().show();

            }

        });

        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = { "苹果", "草莓", "橘子", "冬瓜" };
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("我是列表对话框");
                builder.setItems(items, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
//                        myToast(items[arg1]);
                        Toast.makeText(MainActivity.this,items[arg1],Toast.LENGTH_LONG).show();
                    }
                });
                builder.create().show();

            }
        });

        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setTitle("不带进度条的对话框");
                dialog.setMessage("正在加载中~");
                dialog.show();
            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平样式
                dialog.setTitle("带进度条的对话框");
                dialog.setMessage("加载中~");
                dialog.setMax(200);
                dialog.show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        for (int j = 0; j <= 200; j++) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            dialog.setProgress(j);
                        }

                    }
                }).start();
            }
        });

        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                // 创建一个view，并且将布局加入view中
                View view = LayoutInflater.from(MainActivity.this).inflate(
                        R.layout.dialog_edt, null, false);
                // 将view添加到builder中
                builder.setView(view);
                // 创建dialog
                final Dialog dialog = builder.create();
                // 初始化控件，注意这里是通过view.findViewById
                final EditText edt = (EditText) view.findViewById(R.id.edt);
                Button confirm = (Button) view.findViewById(R.id.confirm);
                Button cancel = (Button) view.findViewById(R.id.cancel);
                // 设置button的点击事件及获取editview中的文本内容
                confirm.setOnClickListener(new android.view.View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        String str = edt.getText() == null ? "" : edt.getText()
                                .toString();
//                        myToast(str);
                        Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
                    }
                });
                // 取消按钮
                cancel.setOnClickListener(new android.view.View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                dialog.show();

            }
        });
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final MyDialog dialog=new MyDialog(MainActivity.this);
                dialog.onNoClickListener= new MyDialog.OnNoClickListener() {
                    @Override
                    public void cancel() {
                      Toast.makeText(MainActivity.this,"no",Toast.LENGTH_LONG).show();
                      dialog.dismiss();
                    }
                };
                dialog.onYesClickListener=new MyDialog.OnYesClickListener() {
                    @Override
                    public void confirm() {
                        Toast.makeText(MainActivity.this,"yes",Toast.LENGTH_LONG).show();
                        dialog.dismiss();
                    }
                };
                dialog.show();

            }
        });
    }
}
